package com.study.demo_cache_redis.service;

import com.study.demo_cache_redis.bean.User;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * @ClassName: UserService
 * @Description:
 * @author: WS
 * @date 2020/12/26
 * @Version: 1.0
 **/
@Service
@CacheConfig(cacheNames = "c1")
public class UserService {
    //    @Cacheable(cacheNames = "c1",keyGenerator = "myKeyGenerator")
//    @Cacheable(cacheNames = "c1",key = "#id")
//    @Cacheable(cacheNames = "c1")
    @Cacheable
    public User getUserById(Integer id) {
        System.out.println("UserService.getUserById>>>" + id);
        User user = new User();
        user.setId(id);
        return user;
    }

    @CacheEvict
    public User deleteUserById(Integer id) {
        System.out.println("UserService.deleteUserById>>>" + id);
        User user = new User();
        return user;
    }

    @CachePut(key = "#user.id")
    public User updateUser(User user) {
        System.out.println("UserService.updateUser>>>" + user.getId());
        return user;
    }
}
